<?php
/**
 * @title 332. 重新安排行程 - 哈希+递归
 * @author start2004
 */

// ini_set("memory_limit", "128M");

class Solution {

    /**
     * @param String[][] $tickets
     * @return String[]
     */
    function findItinerary($tickets) {
        /**
         * @since 2020-08-31 出发地
         */
        $depArray = [];

        /**
         * @since 2020-08-31 迭代
         */
        foreach ($tickets as list($dep, $arr)){
            $depArray[$dep][] = $arr;
        }

        /**
         * @since 2020-08-31 排序
         */
        foreach ($depArray as $dep=>$arrArray){
            sort($arrArray);
            $depArray[$dep] = $arrArray;
        }

        /**
         * @since 2020-08-31 结果集
         */
        $result = [];

        /**
         * @since 2020-08-31 JFK开始
         */
        $result = self::find($depArray, "JFK", count($tickets)+1);

        /**
         * @return
         */
        return $result;
    }

    /**
     * @title 递归查找
     * @author start2004
     * @since 2020-08-31 Created
     *
     * @param array $depArray
     * @param string $dep
     * @param int $len
     */
    public function find($depArray = [], $dep = "", $len = 0){
        /**
         * @since 2020-08-31 只有一个元素
         */
        if($len == 1){
            return [$dep];
        } else {
            if(isset($depArray[$dep])){
                foreach ($depArray[$dep] as $key=>$arr){
                    $nextArray = $depArray;
                    unset($nextArray[$dep][$key]);

                    $result = self::find($nextArray, $arr, $len-1);
                    if($result !== false){
                        array_unshift($result, $dep);
                        return $result;
                    } else {}
                }
                return false;
            } else {
                return false;
            }
        }
    }
}


/**
 * @url http://127.0.0.1/leetcode/202008/2020.08.27.php
 */
$datas = [
    // [["MUC", "LHR"], ["JFK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]],
    // [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]],
    // [["JFK","KUL"],["JFK","NRT"],["NRT","JFK"]],
    [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->findItinerary($data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". $data[1][$i][0] ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
